﻿using Marmat.Foundation;
using Marmat.FrameWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Marmat.Template.WinForm
{
    public class ColumnViewConfiguration : IColumnViewConfiguration
    {
        public virtual bool Configuration(ColumnViewCollection source, out IEnumerable<ColumnView> current)
        {
            if (source is null)
                throw new ArgumentNullException(nameof(source));

            ColumnConfigurationForm frm = new ColumnConfigurationForm();
            foreach (var column in source)
            {
                frm.listBox1.Items.Add(column);
                if (column.Visible)
                    frm.listBox2.Items.Add(column);
            }
            var dialog = frm.ShowDialog();
            current = new List<ColumnView>();
            if (current is IList<ColumnView> items)
            {
                foreach (var item in frm.listBox2.Items)
                {
                    items.Add((ColumnView)item);
                }
            }
            return dialog == DialogResult.OK;
        }

        public virtual IEnumerable<ColumnView> ReadConfiguration(string name)
        {
            string fileName = System.IO.Path.Combine(Application.UserAppDataPath, "layout.xml");
            if (System.IO.File.Exists(fileName))
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(fileName);
                string user_id = SystemCommand.GetUserID();
                var xmlNode = doc.DocumentElement.ChildNodes.OfType<XmlNode>().FirstOrDefault(x => x.Name == "layout" && x.Attributes?["name"].Value == name && x.Attributes?["user_id"].Value == user_id);
                if (xmlNode is not null)
                    return Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<ColumnView>>(xmlNode.InnerText);
            }
            return default;
        }

        public virtual void SaveConfiguration(string name, IEnumerable<ColumnView> view)
        {
            string fileName = System.IO.Path.Combine(Application.UserAppDataPath, "layout.xml");
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            XmlNode xmlNode = null;
            string user_id = SystemCommand.GetUserID();
            if (System.IO.File.Exists(fileName))
            {
                doc.Load(fileName);
                xmlNode = doc.DocumentElement.ChildNodes.OfType<XmlNode>().FirstOrDefault(x => x.Attributes?["name"].Value == name && x.Attributes?["user_id"].Value == user_id);
            }
            else
            {
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(dec);
                xmlNode = doc.CreateElement("view");
                doc.AppendChild(xmlNode);
                xmlNode = null;
            }
            if (xmlNode is null)
            {
                xmlNode = doc.CreateElement("layout");
                doc.DocumentElement.AppendChild(xmlNode);

                var t_name = doc.CreateAttribute("name");
                t_name.Value = name;
                var t_userid = doc.CreateAttribute("user_id");
                t_userid.Value = user_id;
                xmlNode.Attributes.Append(t_name);
                xmlNode.Attributes.Append(t_userid);
            }
            xmlNode.InnerText = Newtonsoft.Json.JsonConvert.SerializeObject(view);
            doc.Save(fileName);
        }
    }
}
